﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLMB.Models;
using QLMB.ViewModels;
using System.Web.Script.Serialization;
using QLMB.Infrastructure;

namespace QLMB.Controllers
{
    public class RouterController : BaseController
    {
        [HttpGet]
        public ActionResult ListFlightNow()
        {
            var info = new FlightInfo();
            info.dschuyenbay = this.Repository.ListAllFlight();
            return View(info);
        }

        [HttpGet]
        public ActionResult GetRoute()
        {
            //var db = new InfoFlight();
            //db.Ngaydi = DateTime.Now.Date;
            //return View(db);
            var info = new RouteInfo();
            info.DSTpDi = info.DSTpDen = this.Repository.LayDSTP();
            info.Ngay = DateTime.Now;
            info.TpDen = " ";
            info.TpDi = " ";
            return View(info);
        }

        [HttpPost]
        public ActionResult GetRoute(RouteInfo info, int dif =0)
        {
            if (info.Ngay <= DateTime.Now.AddDays(3).Date )
            {
                ModelState.AddModelError("Ngay", "Chỉ được phép tạo chuyến bay trước ít nhất 3 ngày");
                info.DSTpDi = this.Repository.LayDSTP();
                info.DSTpDen = this.Repository.LayDSTP();
                return View(info);
            }
            else if (info.Ngay>= DateTime.Now.AddYears(1).Date)
            {
                ModelState.AddModelError("Ngay", "Chỉ được phép tạo chuyến bay trong 1 năm");
                info.DSTpDi = this.Repository.LayDSTP();
                info.DSTpDen = this.Repository.LayDSTP();
                return View(info);
            }
            else 
            {
                int hour = info.Gio;
                info.Ngay = info.Ngay.AddHours(hour).AddMinutes(info.Phut);
                return RedirectToAction("CreateFlight", "Router", info);
            }
            
        }

        [HttpGet]
        public ActionResult CreateFlight(RouteInfo info, int dif= 0)
        {
            //info.MaDb = this.Repository.LayMaDuongBay(info.TpDi, info.TpDen);
            List<SelectListItem> ht = this.Repository.LayHanhTrinh(info.TpDi, info.TpDen);
            info.dsSanBay = new List<FlightItem>();
            foreach (var db in ht)
            {
                info.MaDb = db.Value;
                info.LoaiHanhTrinh = db.Text;
            }
            info.thoigianbay = this.Repository.LayThoiGianBay(info.MaDb);
            if (info.LoaiHanhTrinh.CompareTo("Bay thẳng")==0)
            {
                info.dschuyenbay = this.Repository.LayMaDB_BayThang(info.TpDi, info.TpDen);
                foreach (var db in info.dschuyenbay)
                {
                    info.MaDb = db.Value;
                    info.loaidb = db.Text;
                }
                info.dsSanBay.Add(new FlightItem(info.MaDb,info.TpDi, info.TpDen, "AIRLINE", info.thoigianbay, 0));
                info.dsSanBay.First().NgayDi = info.Ngay;
                info.dsSanBay.First().NgayDen = info.Ngay.AddMinutes((double)info.thoigianbay);
                info.DsMayBay = this.Repository.LayDSMayBay(info.loaidb, info.dsSanBay.First().NgayDi, info.dsSanBay.First().NgayDen);
            }
            else 
            {
                //info.dschuyenbay = this.Repository.LayMaDB_HopTac(info.TpDi, info.TpDen);
                info.dsSanBay = this.Repository.LaySanBay(info.MaDb);
                info.loaidb = "Quoc te";
                //info.DsMayBay = this.Repository.LayDSMayBay(info.loaidb);
                int i = 0;
                DateTime temp = DateTime.Now.Date;
                foreach (var sb in info.dsSanBay)
                {
                    
                    if(i==0)
                    {
                        sb.NgayDi = info.Ngay;
                        sb.NgayDen = info.Ngay.AddMinutes((double)sb.ThoiGianBay);
                    }
                    else
                    {
                        sb.NgayDi = temp.AddHours(3);
                        sb.NgayDen = sb.NgayDi.AddMinutes((double)sb.ThoiGianBay);
                    }
                    if (sb.HangKhaiThac == "AIRLINE")
                    {
                        sb.Maybay = 1;
                        info.DsMayBay = this.Repository.LayDSMayBay(info.loaidb, sb.NgayDi, sb.NgayDen);
                    }
                    temp = sb.NgayDen;
                    i++;
                    
                }
            }
            TempData["FlightInfo"] = info;
            return View(info);
        }
        [HttpPost]
        public ActionResult CreateFlight(RouteInfo info)
        {
            string mb = info.MayBay;
            info = (RouteInfo)TempData["FlightInfo"];
            info.MayBay = mb;
            TempData["FlightInfo"] = info;
            return RedirectToAction("Submit", "Router", info);
        }

        [HttpGet]
        public ActionResult ViewFlight(CHUYENBAY cb)
        {
            
            if (cb.NgayDen != null)
            {
                
                FlightItem fitem = this.Repository.LayThongTinChuyenBay(cb);
                return View(fitem);
            }
            return View();
           
        }
        [HttpGet]
        public ActionResult Submit(RouteInfo info)
        {
            info = (RouteInfo)TempData["FlightInfo"];
            if (info.dsSanBay.Count == 1) this.Repository.CreateFlight(info.dsSanBay.First(), info.MayBay,0);
            else
            {
                int num=1;
                string macbchinh = this.Repository.CreateFlight(info.Ngay, info.MaDb);
                foreach (var cb in info.dsSanBay)
                {
                    this.Repository.CreateFlight(cb, info.MayBay, 1,macbchinh,num);
                    num++;
                }
            }
            
            return View();
        }

        public JsonResult GetListArriveAirport(string id)
        {
            RouteInfo temp = new RouteInfo();
            var TpDen = this.Repository.LayDSTPDen(id);
            return Json(TpDen, JsonRequestBehavior.AllowGet);
        }


    }    
}
